package application;

import Base.ResourceTest;
import com.huawei.translate.model.Application;
import com.huawei.translate.model.Renter;
import com.huawei.translate.vo.application.ApplicationDto;
import com.huawei.translate.vo.application.ApplicationsDto;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RunWith(SpringRunner.class)
public class When_get_application extends ResourceTest{

    private Application application;

    @Before
    public void setup(){
        application = new Application();
        application.setAppName("welink");
        application.setOnlineDate(new Date());
        application.setAccessCountPerSec("100");
        application.setLanguages("en;zh");
        application.setDescription("HelloWorld!!");
        application.setProductOwner("yyy");
        application.setPoEmail("po@huawei.com");
        application.setInterfacePerson("interface");
        application.setiPersonEmail("inter@163.com");

        Renter renter = new Renter();
        renter.setAppKey("HelloWorld");
        renter.setCategory("CustomCategory");
        renter.setAppSecret(UUID.randomUUID().toString());
        application.setRenter(renter);
        applicationRepository.save(application);
    }


    @Test
    public void should_return_applications_when_request_all() {
        
        ResponseEntity<ApplicationsDto> response = this.testRestTemplate.getForEntity("/api/applications",ApplicationsDto.class);

        Assert.assertEquals(HttpStatus.OK,response.getStatusCode());

        List<ApplicationDto> list = response.getBody().getList();

        ApplicationDto dto = list.get(0);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Assert.assertEquals(1, list.size());
        Assert.assertEquals(application.getLanguages(), dto.getLanguages());
        Assert.assertEquals(application.getProductOwner(), dto.getProductOwner());
        Assert.assertEquals(application.getInterfacePerson(), dto.getInterfacePerson());
        Assert.assertEquals(sdf.format(application.getCreatedAt()),sdf.format(dto.getApplyDate()));
        Assert.assertEquals(sdf.format(application.getOnlineDate()),sdf.format(dto.getOnlineDate()));
        Assert.assertEquals(0,dto.getStatus());
        Assert.assertEquals(application.getRenter().getAppKey(),dto.getAppKey());
        Assert.assertEquals(application.getRenter().getCategory(),dto.getType());
        Assert.assertEquals(application.getRenter().getAppSecret(),dto.getAppSecret());
    }

    @Test
    public void should_return_application_by_Id() {
        ResponseEntity<ApplicationDto> response = this.testRestTemplate.getForEntity("/api/applications/" + application.getId(), ApplicationDto.class);
        ApplicationDto dto = response.getBody();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        Assert.assertEquals(application.getAppName(), dto.getAppName());
        Assert.assertEquals(application.getRenter().getAppKey(), dto.getAppKey());
        Assert.assertEquals(application.getRenter().getAppSecret(), dto.getAppSecret());
        Assert.assertEquals(sdf.format(application.getOnlineDate()), sdf.format(dto.getOnlineDate()));
        Assert.assertEquals(application.getAccessCountPerSec(), dto.getAccessCountPerSec());
        Assert.assertEquals(application.getLanguages(), dto.getLanguages());
        Assert.assertEquals(application.getDescription(), dto.getDescription());
        Assert.assertEquals(application.getRenter().getCategory(), dto.getType());
        Assert.assertEquals(application.getRenter().getCategory(), dto.getType());
        Assert.assertEquals(application.getProductOwner(), dto.getProductOwner());
        Assert.assertEquals(application.getPoEmail(), dto.getProductOwnerEmail());
        Assert.assertEquals(application.getInterfacePerson(), dto.getInterfacePerson());
        Assert.assertEquals(application.getiPersonEmail(), dto.getInterfacePersonEmail());
        Assert.assertEquals(application.getStatus(), dto.getStatus());
    }
}